<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>和可以被 K 整除的子数组</title>
</head>
<body>
  <script>
    function fn(nums, K) {
      let res = 0
      let count = 0
      const map = new Array(K).fill(0)
      map[0] = 1
      for (let i = 0; i < nums.length; i++) {
        res = (res + nums[i]) % K
        if (res < 0) {
          res += K
        }
        count += map[res] // 索引对应模的结果，值对应出现次数
        map[res]++
      }
      return count
    }
    console.log(fn([4, 5, 0, -2, -3, 1], 5))
  </script>
</body>
</html>